package com.module.controller;


import com.module.framework.domain.Book;
import com.module.framework.domain.ResponseResult;
import com.module.framework.dto.BookListDTO;
import com.module.framework.service.BookService;
import com.module.framework.vo.BookDetailVO;
import com.module.framework.vo.PageVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/list")
    public ResponseResult productList(@RequestBody BookListDTO bookListDTO) {
        PageVO pageVO = bookService.bookList(bookListDTO);
        return ResponseResult.okResult(pageVO);
    }

    @GetMapping("/info")
    public ResponseResult getBookInfo(Integer id) {
        BookDetailVO bookInfo = bookService.getBookInfo(id);
        return ResponseResult.okResult(bookInfo);
    }

    @PostMapping("/add")
    public ResponseResult addBook(@RequestParam(value = "file", required = false) MultipartFile file, String data) throws IOException {
        bookService.addBook(file, data);
        return ResponseResult.okResult();
    }

    @PostMapping("/update")
    public ResponseResult updateBook(@RequestBody Book book) {
        bookService.updateBook(book);
        return ResponseResult.okResult();
    }

    @GetMapping("/delete")
    public ResponseResult deleteBook(Integer id) {
        bookService.deleteBook(id);
        return ResponseResult.okResult();
    }

    @GetMapping("/lend")
    public ResponseResult lendBook(Integer id, Long userId) {
        String num = bookService.lendBook(id, userId);
        return ResponseResult.okResult(num);
    }


}
